package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.IFacilitatorDAO;
import org.jsola.hr.dto.FacilitatorAddDTO;
import org.jsola.hr.dto.FacilitatorUpdateDTO;
import org.jsola.hr.entity.FacilitatorDO;
import org.jsola.hr.query.FacilitatorQuery;
import org.jsola.hr.service.IFacilitatorService;
import org.jsola.hr.vo.FacilitatorListVO;
import org.jsola.hr.vo.FacilitatorVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 卫人云-服务商
 *
 * @author zhr
 */
@Service("hrFacilitatorServiceImpl")
public class FacilitatorServiceImpl implements IFacilitatorService {

    @Autowired
    private IFacilitatorDAO facilitatorDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public FacilitatorVO save(FacilitatorAddDTO facilitatorAddDTO, TokenUser tokenUser) {
        // 转DO
        FacilitatorDO facilitatorDO = facilitatorAddDTO.to(FacilitatorDO.class);
        // 保存
        facilitatorDO = save(facilitatorDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return facilitatorDO.to(FacilitatorVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(FacilitatorUpdateDTO facilitatorUpdateDTO, TokenUser tokenUser) {
        //转DO
        FacilitatorDO facilitatorDO = facilitatorUpdateDTO.to(FacilitatorDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(facilitatorDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... facilitatorIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) facilitatorIds);
    }


    @Override
    public FacilitatorVO selectById(String facilitatorId, String siteId) {
        FacilitatorDO facilitatorDO = selectDOById(facilitatorId, siteId);
        if (facilitatorDO == null) {
            return null;
        }
        return facilitatorDO.to(FacilitatorVO.class);
    }

    @Override
    public List<FacilitatorListVO> select(FacilitatorQuery facilitatorQuery, String siteId) {
        List<FacilitatorDO> facilitatorDOList = selectDO(facilitatorQuery, siteId);
        if(CollectionUtils.isEmpty(facilitatorDOList)) {
            return facilitatorDOList == null ? null : new ArrayList<>();
        }
        return facilitatorDOList.stream()
                .map(facilitatorDO -> facilitatorDO.to(FacilitatorListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(FacilitatorQuery facilitatorQuery, String siteId) {
        Example example = buildExample(facilitatorQuery,siteId);
        return facilitatorDAO.selectCountByExample(example);
    }

    @Override
    public Page<FacilitatorListVO> selectPage(FacilitatorQuery facilitatorQuery, String siteId) {
        Example example = buildExample(facilitatorQuery,siteId);
        Page<FacilitatorDO> page = facilitatorDAO.selectPageByExample(example,
                facilitatorQuery.getPageNo(),
                facilitatorQuery.getPageSize());

        return page.to(FacilitatorListVO.class);
    }

    @Override
    public FacilitatorDO selectDOById(String facilitatorId, String siteId) {
        return listById(facilitatorId, siteId);
    }

    @Override
    public List<FacilitatorDO> selectDO(FacilitatorQuery facilitatorQuery, String siteId) {
        Example example = buildExample(facilitatorQuery,siteId);
        return facilitatorDAO.selectByExample(example);
    }


    /**
     * 根据查询参数，构建example

     * @param facilitatorQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(FacilitatorQuery facilitatorQuery, String siteId) {
        Example example = new Example(FacilitatorDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        // 排序
        ExampleKit.setExampleOrder(example,facilitatorQuery.getOrders());
        return example;
    }
}




